<template>
  <div id="home" class="wrapper">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control :titles="['流行','新款','精选']"
                 @tabClick="tabClick"
                 ref="tabControl1"
                 class="tab-control"
                 v-show="isTabFixed"/>
    <scroll class="content"
            ref="scroll"
            :probe-type="2"
            @scrollP="scrollP"
            :pull-up-load="true"
            @pullingUp="pullingUp">
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"/>
      <recommend-view :recommends="recommends"/>
      <feature-view/>
      <tab-control :titles="['流行','新款','精选']"
                   @tabClick="tabClick"
                   ref="tabControl2"/>
      <goods-list :goods="showGoods"/>
    </scroll>

    <back-top @click.native="backClick" v-show="isShowBackTop"/>
  </div>
</template>

<script>
import {getHomeMultidata, getHomeGoods} from '@/network/home'
import Scroll from '@/components/common/scroll/Scroll'

import NavBar from '@/components/common/navbar/NavBar'
import HomeSwiper from '@/views/home/childComps/HomeSwiper'
import RecommendView from '@/views/home/childComps/RecommendView'
import FeatureView from '@/views/home/childComps/FeatureView'
import TabControl from '@/components/content/tabControl/TabControl'
import GoodsList from '@/components/content/goods/GoodsList'
import BackTop from '@/components/content/backtop/BackTop'
import {backTopMixin, itemListenerMixin} from '@/common/mixin'

export default {
  name: "Home",
  components: {
    NavBar,
    HomeSwiper,
    RecommendView,
    FeatureView,
    TabControl,
    GoodsList,
    Scroll,
    BackTop
  },
  mixins: [itemListenerMixin, backTopMixin],
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        'pop': {page: 0, list: []},
        'new': {page: 0, list: []},
        'sell': {page: 0, list: []},
      },
      currentIndex: 'pop',
      tabOffsetTop: 0,
      isTabFixed: false,
      saveY: 0,
    }
  },
  computed: {
    showGoods() {
      return this.goods[this.currentIndex].list
    }
  },
  created() {
    // 1. 请求多个数据
    this.getHomeMultidata()

    // 2. 请求商品数据
    this.getHomeGoods('pop')
    this.getHomeGoods('new')
    this.getHomeGoods('sell')
  },
  mounted() {
  },
  activated() {
    this.$refs.scroll.scrollTo(0, this.saveY, 10)
    this.$refs.scroll.refresh()
  },
  deactivated() {
    // 保存Y值
    this.saveY = this.$refs.scroll.getScrollY()

    // 取消事件总线的监听
    this.$bus.$off('itemImageLoad', this.itemImgListener)
  },
  methods: {
    /**
     * 事件监听相关的方法
     */
    tabClick(index) {
      // 根据用户的点击获取index，并绑定对应的名字和下标
      switch (index) {
        case 0:
          this.currentIndex = 'pop'
          break
        case 1:
          this.currentIndex = 'new'
          break
        case 2:
          this.currentIndex = 'sell'
          break
      }
      this.$refs.tabControl1.currentIndex = index
      this.$refs.tabControl2.currentIndex = index
    },
    scrollP(position) {
      // 1. 判断BackTop是否显示
      this.isShowBackTop = (-position.y) > 800

      // 2. 决定tabControl是否吸顶
      this.isTabFixed = (-position.y) > this.tabOffsetTop
    },
    pullingUp() {
      this.getHomeGoods(this.currentIndex)
    },
    swiperImageLoad() {
      // 获取tabControl的offsetTop
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop
    },

    /**
     * 网络请求相关方法
     */
    getHomeMultidata() {
      getHomeMultidata().then(res => {
        this.banners = res.data.banner.list
        this.recommends = res.data.recommend.list
      })
    },
    // 上拉加载更多：
    getHomeGoods(type) {
      // 把当前的page记录下来
      const page = this.goods[type].page + 1
      getHomeGoods(type, page).then(res => {
        // 把新请求来的数据添加到list中
        this.goods[type].list.push(...res.data.list)
        // 加载完成后 page + 1
        this.goods[type].page += 1

        // 完成上拉加载更多
        this.$refs.scroll.finishPullUp()

        // todo 自己理解的添加
        this.$refs.scroll.refresh()
      })
    }
  }
}
</script>

<style scoped>
#home {
  /*padding-top: 44px;*/
  height: 100vh;

  position: relative;
}

.home-nav {
  background-color: var(--color-tint);
  /*背景颜色: 之前定义好的css变量--color-tint*/
  color: #fff;

  /*在使用浏览器原生滚动时, 为了让导航不跟随一起滚动*/
  /*position: fixed;*/
  /*left: 0;*/
  /*right: 0;*/
  /*top: 0;*/
  /*z-index: 9;*/
}

.tab-control {
  /*
 position: sticky;
 top: 44px;
 z-index: 9;

 /osition: sticky ---> static + fixed
 当距离不到44px时，自动设置为static
 当距离top: 44px，自动设置为fixed，实现吸顶效果
 */
  position: relative;
  z-index: 9;
  top: -2px;
}

/*content内容高度计算，方法一*/
.content {
  overflow: hidden;

  position: absolute;
  top: 43px;
  bottom: 49px;
  left: 0;
  right: 0;
}

/*content内容高度计算，方法二*/
/*.content {*/
/*height: calc(100% - 93px);*/
/*overflow: hidden;*/
/*margin-top: 44px;*/
/*}*/

</style>
